$(document).ready(function() {
	var fruit = $('#fruit');
	//var seedTotal = $("#seedTotal");
	var seedFirst = $("#seedFirst");
	var seedingTotal = $('#seedingTotal');
	var gaidiv = $(".gaidiv");
	var aform = $("#aform");

	$(".di").bind("click", function() {
		var _this = $(this);
		_this.unbind("click");
		if(msico == "kaiico" && _this.html().indexOf("diinfo") == -1) {
			var id = _this.attr('id');
			var sort = id.replace("di", ""); //土地的ID
			var price = 0;
			var type = 1;

			var seedFirstVal = parseInt(seedFirst.val());
			if(sort < 11) {
				if(seedFirstVal < 300) {
					alert("您没有这么多种子！快去购买花吧~~！");
					return false;
				}
				price = 300;
			} else {
				if(seedFirstVal < 3000) {
					alert("您没有这么多种子！快去购买花吧~~！");
					return false;
				}
				price = 3000;
				type = 2;
			}

			tishi2();

			$.post("/seedingFirst/create", {
				sort: sort,
				price: price,
				type: type
			}, function(result) {
				tishi2close();

				if(result['code'] != 0) {
					layer.msg(result['message']);
					return false;
				}

				var newSeedFirstVal = seedFirstVal - price;
				seedFirst.val(newSeedFirstVal);
				$('#seed').text(newSeedFirstVal);

				var seedingTotalVal = parseInt(seedingTotal.text());
				var newSeedingTotalVal = seedingTotalVal + price;
				seedingTotal.text(newSeedingTotalVal);

				//seedTotal.text(newSeedFirstVal + newSeedingTotalVal);

				_this.html("<img src='/" + imgsrc + "/shu1" + imgt + "' /><div class='diinfo'>" + price + "颗</div>");

				if(sort >= 11) {
					_this.css({
						background: "url(/" + imgsrc + "/dibg11" + imgt + ") top left no-repeat"
					});
				} else {
					_this.css({
						background: "url(/" + imgsrc + "/dibg01" + imgt + ") top left no-repeat"
					});
				}

			});
		} else if(msico == "jiaico" && _this.html().indexOf("diinfo") > -1) {
			var id = _this.attr('id');
			var sort = id.replace("di", ""); //土地的ID
			var seedFirstVal = parseInt(seedFirst.val());
			var maxAdd = $('#maxAdd');
			var currentPrice = parseInt(_this.find('.diinfo').text());

			if(sort < 11) {
				if(currentPrice < 3000) {
					maxAdd.text(3000 - currentPrice);
				} else {
					maxAdd.text(0);
				}
			} else {
				if(currentPrice < 30000) {
					maxAdd.text(30000 - currentPrice);
				} else {
					maxAdd.text(0);
				}
			}

			gaidiv.fadeTo("fast",0.3);
			aform.fadeTo("fast",1);

			$("#addPriceBtn").click(function() {
				var _thisBtn = $(this);
				_thisBtn.attr({"disabled":"disabled"});

				var price = parseInt($('#addPrice').val());
				if(seedFirstVal < price) {
					layer.msg("您没有这么多种子！快去购买花吧~~！");
					_thisBtn.removeAttr('disabled');
					return false;
				}

				if(price <= 0 || price - seedFirstVal > 30000 - currentPrice) {
					layer.msg('请输入正确的数量');
					_thisBtn.removeAttr('disabled');
					return false;
				}
				$("#closes").click();

				tishi2();
				$.post('/seedingFirst/addSeed', {price: price, sort: sort}, function(result) {
					tishi2close();
					_thisBtn.removeAttr('disabled');

					if(result['code'] != 0) {
						layer.msg(result['message']);
						return false;
					}

					location.reload();
				});
			});
		} else if(msico=="caiico" && _this.html().indexOf("diinfo")>0){
			var id = _this.attr('id');
			var sort = id.replace("di", ""); //土地的ID

			tishi2();
			$.post('/seedingFirst/getSeed', {sort: sort}, function(result) {
				tishi2close();

				if(result['code'] != 0) {
					layer.msg(result['message']);
					return false;
				}

				var seedFirstVal = parseInt(seedFirst.val());
				var diinfo = _this.find('.diinfo');
				var price = parseInt(diinfo.text());


				var seedingTotalVal = parseInt(seedingTotal.text());
				var newSeedingTotalVal = seedingTotalVal - price;
				seedingTotal.text(newSeedingTotalVal);
				
				var fruitVal = parseInt(fruit.text());
				var newFruitVal = fruitVal + price;
				fruit.text(newFruitVal);

				//seedTotal.html(newFruitVal + seedFirstVal + newSeedingTotalVal);

				_this.find('img').remove();
				diinfo.remove();

				if(sort >= 11) {
					_this.css({
						background: "url(/" + imgsrc + "/dibg10" + imgt + ") top left no-repeat"
					});
				} else {
					_this.css({
						background: "url(/" + imgsrc + "/dibg00" + imgt + ") top left no-repeat"
					});
				}
			});
		}
	});

	$("#closes").click(function(){
		gaidiv.css({display:"none"});
		aform.css({display:"none"});
	});
});